package com.flute.tools.timer;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * timer控制的操作
 * 
 * @author Dave
 * 
 */
public class ReflectionMethod {
	/**
	 * 类名
	 */
	private String className;
	/**
	 * 方法名
	 */
	private String methodName;
	/**
	 * 实例
	 */
	private Object instance;

	private Object[] params;

	private Class<?>[] classes;

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	/**
	 * 运行该操作
	 */
	public void run() {
		try {
			Object obj = null;
			Class<?> cl = null;
			if (instance == null) {
				cl = Class.forName(className);
				obj = cl.newInstance();
			} else {
				cl = instance.getClass();
				obj = instance;
			}
			if (classes == null) {
				Method mt = cl.getMethod(methodName, new Class<?>[] {});
				mt.invoke(obj, new Object[] {});
			} else {
				Method mt = cl.getMethod(methodName, classes);
				mt.invoke(obj, params);
			}
			FlexTimer.logger.info("The method '" + methodName + "' in class '"
					+ className + "' is called successfully");
		} catch (ClassNotFoundException e) {
			FlexTimer.logger.error("Load class '" + className + "' faield.\n"
					+ getExceptionContent(e));
		} catch (InstantiationException e) {
			FlexTimer.logger.error("Instance class '" + className
					+ "' faield.\n" + getExceptionContent(e));
		} catch (IllegalAccessException e) {
			FlexTimer.logger.error("Access constructor of class '" + className
					+ "'  faield.\n" + getExceptionContent(e));
		} catch (SecurityException e) {
			FlexTimer.logger.error("Throw method security exception at class '"
					+ className + "' method '" + methodName + "'.\n"
					+ getExceptionContent(e));
		} catch (NoSuchMethodException e) {
			FlexTimer.logger.error("No '" + methodName + "' method in class '"
					+ className + "'  faield.\n" + getExceptionContent(e));
		} catch (IllegalArgumentException e) {
			FlexTimer.logger.error("Illegal argument for class '" + className
					+ "' method '" + methodName + "'.\n"
					+ getExceptionContent(e));
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			FlexTimer.logger
					.error("Throw InvocationTarget exception when invoke  class '"
							+ className
							+ "' method '"
							+ methodName
							+ "'.\n"
							+ getExceptionContent(e));
		}
	}

	public Object getInstance() {
		return instance;
	}

	public void setInstance(Object instance) {
		this.instance = instance;
	}

	public Object[] getParams() {
		return params;
	}

	public void setParams(Object[] params) {
		this.params = params;
	}

	public Class<?>[] getClasses() {
		return classes;
	}

	public void setClasses(Class<?>[] classes) {
		this.classes = classes;
	}

	private String getExceptionContent(Exception e) {
		StringWriter sw = new StringWriter();
		e.printStackTrace(new PrintWriter(sw));
		String content = sw.toString();
		try {
			sw.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		return content;
	}
}
